package cn.edu.zjitc.servlet.code.ex04;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet("/requestDemo")
public class RequestDemoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String[] hobbies = req.getParameterValues("hobby");
        String method = req.getMethod();
        String uri = req.getRequestURI();
        String userAgent = req.getHeader("User-Agent");

        resp.getWriter().write("用户名：" + username + "<br>爱好：" + Arrays.toString(hobbies)
                + "<br>请求方式：" + method + "<br>URI：" + uri + "<br>User-Agent：" + userAgent);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}